<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Job列表</title>
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!--[if lt IE 9]>
     <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
     <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
	<table class="table table-hover table-bordered table-striped ">
		<tr>
			<th nowrap>Trigger 名称</th>
			<th nowrap>Trigger 分组</th>
			<th nowrap>开始时间</th>
			<th nowrap>结束时间</th>
			<th nowrap>上次执行时间</th>
			<th nowrap>下次执行时间</th>
			<th nowrap>优先级</th>
			<th nowrap>Trigger 状态</th>
			<th nowrap>Trigger 类型</th>
			<th nowrap>描述</th>
			<th nowrap>动作命令</th>
		</tr>
		<c:forEach var="trigger" items="${triggerList}">
			<tr>
				<td nowrap><a class="updateTrigger" href="#" data-key='{"triggerGroup":"${trigger.triggerGroup}","triggerName":"${trigger.triggerName}"}'>${trigger.triggerName}</a></td>
				<td nowrap>${trigger.triggerGroup}</td>
				<td nowrap>${trigger.startTimeStr}</td>
				<td nowrap>${trigger.endTimeStr}</td>
				<td nowrap>${trigger.prevFireTimeStr}</td>
				<td nowrap>${trigger.nextFireTimeStr}</td>
				<td nowrap>${trigger.priority}</td>
				<td nowrap>${trigger.triggerState}</td>
				<td nowrap>${trigger.triggerType}</td>
				<td nowrap>${trigger.description}</td>
				<td nowrap>
						<a href="#" class="operateTrigger" data-key='{"operate":"execute","triggerGroup":"${trigger.triggerGroup}","triggerName":"${trigger.triggerName}"}'>执行</a>
					<c:if test="${trigger.triggerState eq 'ACQUIRED'}">
						<a href="#" class="operateTrigger" data-key='{"operate":"pause","triggerGroup":"${trigger.triggerGroup}","triggerName":"${trigger.triggerName}"}'>暂停</a>
					</c:if>
					<c:if test="${trigger.triggerState eq 'PAUSED' || trigger.triggerState eq 'WAITING'}">
						<a href="#" class="operateTrigger" data-key='{"operate":"resume","triggerGroup":"${trigger.triggerGroup}","triggerName":"${trigger.triggerName}"}'>恢复</a>
					</c:if>
					<a href="#" class="operateTrigger" data-key='{"operate":"delete","triggerGroup":"${trigger.triggerGroup}","triggerName":"${trigger.triggerName}"}'>删除</a>
				</td>
			</tr>
		</c:forEach>
	</table>


	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title">请输入CRON表达式</h4>
				</div>
				<div class="modal-body ">
	    					<input id="cronExpression" type="text" name="cronExpression" />
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-primary" id="submit_update">确定</button>
				</div>
			</div>
		</div>
	</div>

</body>
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="http://cdn.bootcss.com/bootbox.js/4.3.0/bootbox.min.js"></script>

<script type="text/javascript">

	$(".updateTrigger").click(function(e) {

		e.preventDefault();

		var triggerGroup = $(this).data('key').triggerGroup;
		var triggerName = $(this).data('key').triggerName;

		$.ajax({
			type : 'POST',
			url : '<c:url value="/job/cron" />',
			data : {
				triggerGroup : triggerGroup,
				triggerName : triggerName
			},
			dataType : 'json',
			error : function() {
				bootbox.alert('error');
			},
			success : function(data) {
				$('#cronExpression').val(data.cronExpression);
				$('#myModal').modal('show');
			}
		});

		$('#myModal').data('triggerGroup', triggerGroup);
		$('#myModal').data('triggerName', triggerName);
	});

	$("#submit_update").click(function() {
		
		var cronExpression = $('#cronExpression').val();

		if (cronExpression == '') 
		{
			bootbox.alert('cronExpression不能为空，亲！');
			return;
		}
		
		var triggerGroup = $('#myModal').data('triggerGroup');
		var triggerName = $('#myModal').data('triggerName');
		
		$.ajax({
			type : 'POST',
			url : '<c:url value="/job/update" />',
			data : {
				triggerGroup : triggerGroup,
				triggerName : triggerName,
				cronExpression : cronExpression
			},
			dataType : 'json',
			error : function() {
				bootbox.alert('error');
			},
			success : function(data) {
				if (data.result == true) 
				{
					bootbox.alert('修改成功！',function(){
						location.reload();
					});
				} 
				else 
				{
					bootbox.alert('error');
				}
			}
		});
	});
	
	$(".operateTrigger").click(function(e){
		
		e.preventDefault();
		
		var a = $(this);
		var operate = $(this).data('key').operate;
		var triggerGroup = $(this).data('key').triggerGroup;
		var triggerName = $(this).data('key').triggerName;
		
		$.ajax({
			type : 'POST',
			url : '<c:url value="/job/operate"/>',
			data:{
				operate : operate,
				triggerGroup : triggerGroup,
				triggerName : triggerName
			},
			beforeSend : function() {
				$(a).parent().html("<img src=\"<c:url value='/img/loading2.gif'/>\" />");
			},
			error : function() {
				bootbox.alert("执行失败！",function(){
					window.location.reload();
				});
			},
			success : function() {
				bootbox.alert("执行成功！",function(){
					window.location.reload();
				});
			}
		});
		
	});

</script>
</html>